class BeerListController < UITableViewController
  def init
    if super
      self.tabBarItem = UITabBarItem.alloc.initWithTitle("List", image: UIImage.imageNamed("list.png"), tag: 1)
    end
    self
  end

  def viewDidLoad
    view.dataSource = view.delegate = self
  end

  def viewWillAppear(animated)
    # 取消 NavigationController 中的 header, 避免遮挡了 table 中的数据
    navigationController.setNavigationBarHidden(true, animated:true)
  end    


  # 使用相同的方法 tableView 以及不同的参数 numberOf... cellForRow... 来组织 table 中的数据
  def tableView(tableView, numberOfRowsInSection: section)
    Beer::All.size
  end

  CELLID = 'CellIdentifier'
  def tableView(tableView, cellForRowAtIndexPath: indexPath)
    cell = tableView.dequeueReusableCellWithIdentifier(CELLID) || begin
      cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:CELLID)
      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
      cell.selectionStyle = UITableViewCellSelectionStyleNone
      cell
    end

    beer = Beer::All[indexPath.row]
    cell.textLabel.text = beer.title
    cell
  end

  def tableView(tableView, accessoryButtonTappedForRowWithIndexPath: indexPath) 
    beer = Beer::All[indexPath.row]
    controller = UIApplication.sharedApplication.delegate.beer_details_controller
    controller.showDetailsForBeer(beer)
  end
end